﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace PosClient.Model
{
    public class Plu: INotifyPropertyChanged
    {

        public enum PluTag
        {
            Normal = 1, // 正常
            Promote = 2, // 促销
            Cut = 3, // 削价
            Present = 4, //赠送
            Change = 5, //改价
            Vip = 6, //会员卡折扣
            ExChange = 7,//换购
        }


        private string _PluCode;
        private string _PluName;
        private string _Barcode;
        private Image _PluImage;
        private string _PluSvg;
        private decimal _Amount;
        private decimal _Count;
        private decimal _DisAmount;
        private PluTag _Tag = Plu.PluTag.Normal;
        private decimal _OriginAmount;
        private decimal _OriginPrice;
        private decimal _VipPrice;
        private decimal _Price;
        private decimal _PromotePrice1;
        private decimal _PromotePrice2;
        private decimal _PromotePrice3;
        private decimal _PromotePrice4;
        private string _CanDiscount;
        private long _PluId;
        private List<string> _TagList;
        private int _Seq;
        private int _PluType;
        private long _Promote1;
        private long _Promote2;
        private long _Promote3;
        private long _Promote4;


        public int Seq
        {
            get => _Seq;
            set
            {
                if (value != _Seq)
                {
                    _Seq = value;
                    NotifyPropertyChanged();
                }
            }
        }

        public long PluId
        {
            get => _PluId;
            set
            {
                if (value != _PluId)
                {
                    _PluId = value;
                    NotifyPropertyChanged();
                }
            }
        }
        public string PluCode
        {
            get => _PluCode;
            set
            {
                if (value != _PluCode)
                {
                    _PluCode = value;
                    NotifyPropertyChanged();
                }
            }
        }

        public string PluName
        {
            get => _PluName;
            set
            {
                if (value != _PluName)
                {
                    _PluName = value;
                    NotifyPropertyChanged();
                }
            }
        }

        public string Barcode
        {
            get => _Barcode;
            set
            {
                if (value != _Barcode)
                {
                    _Barcode = value;
                    NotifyPropertyChanged();
                }
            }
        }

        public Image PluImage
        {
            get => _PluImage;
            set
            {
                if (value != _PluImage)
                {
                    _PluImage = value;
                    NotifyPropertyChanged();
                }
            }
        }

        public string PluSvg
        {
            get => _PluSvg;
            set
            {
                if (value != _PluSvg)
                {
                    _PluSvg = value;
                    NotifyPropertyChanged();
                }
            }
        }

        public decimal Amount
        {
            get => _Amount;
            set
            {
                if (value != _Amount)
                {
                    _Amount = value;
                    NotifyPropertyChanged();
                }
            }
        }

        public decimal Count
        {
            get => _Count;
            set
            {
                if (value != _Count)
                {
                    _Count = value;
                    NotifyPropertyChanged();
                }
            }
        }

        public decimal DisAmount
        {
            get => _DisAmount;
            set
            {
                if (value != _DisAmount)
                {
                    _DisAmount = value;
                    NotifyPropertyChanged();
                }
            }
        }

        public decimal Price
        {
            get => _Price;
            set
            {
                if (value != _Price)
                {
                    _Price = value;
                    NotifyPropertyChanged();
                }
            }
        }

        public decimal OriginAmount
        {
            get => _OriginAmount;
            set
            {
                if (value != _OriginAmount)
                {
                    _OriginAmount = value;
                    NotifyPropertyChanged();
                }
            }
        }

        public decimal OriginPrice
        {
            get => _OriginPrice;
            set
            {
                if (value != _OriginPrice)
                {
                    _OriginPrice = value;
                    NotifyPropertyChanged();
                }
            }
        }

        public decimal VipPrice
        {
            get => _VipPrice;
            set
            {
                if (value != _VipPrice)
                {
                    _VipPrice = value;
                    NotifyPropertyChanged();
                }
            }
        }

        public decimal PromotePrice1
        {
            get => _PromotePrice1;
            set
            {
                if (value != _PromotePrice1)
                {
                    _PromotePrice1 = value;
                    NotifyPropertyChanged();
                }
            }
        }

        public decimal PromotePrice2
        {
            get => _PromotePrice2;
            set
            {
                if (value != _PromotePrice2)
                {
                    _PromotePrice2 = value;
                    NotifyPropertyChanged();
                }
            }
        }

        public decimal PromotePrice3
        {
            get => _PromotePrice3;
            set
            {
                if (value != _PromotePrice3)
                {
                    _PromotePrice3 = value;
                    NotifyPropertyChanged();
                }
            }
        }

        public decimal PromotePrice4
        {
            get => _PromotePrice4;
            set
            {
                if (value != _PromotePrice4)
                {
                    _PromotePrice4 = value;
                    NotifyPropertyChanged();
                }
            }
        }

        public PluTag Tag
        {
            get => _Tag;
            set
            {
                if (value != _Tag)
                {
                    _Tag = value;
                    NotifyPropertyChanged();
                }
            }
        }

        public string CanDiscount
        {
            get => _CanDiscount;
            set
            {
                if (value != _CanDiscount)
                {
                    _CanDiscount = value;
                    NotifyPropertyChanged();
                }
            }
        }

        public int PluType
        {
            get => _PluType;
            set
            {
                if (value != _PluType)
                {
                    _PluType = value;
                    NotifyPropertyChanged();
                }
            }
        }

        public long Promote1
        {
            get => _Promote1;
            set
            {
                if (value != _Promote1)
                {
                    _Promote1 = value;
                    NotifyPropertyChanged();
                }
            }
        }

        public long Promote2
        {
            get => _Promote2;
            set
            {
                if (value != _Promote2)
                {
                    _Promote2 = value;
                    NotifyPropertyChanged();
                }
            }
        }

        public long Promote3
        {
            get => _Promote3;
            set
            {
                if (value != _Promote3)
                {
                    _Promote3 = value;
                    NotifyPropertyChanged();
                }
            }
        }

        public long Promote4
        {
            get => _Promote4;
            set
            {
                if (value != _Promote4)
                {
                    _Promote4 = value;
                    NotifyPropertyChanged();
                }
            }
        }

        public List<string> TagList
        {
            get => _TagList;
            set
            {
                if (value != _TagList)
                {
                    _TagList = value;
                    NotifyPropertyChanged();
                }
            }
        }

        public void Summary()
        {
            Amount = GetBestAmount();
            OriginAmount = Math.Round(OriginPrice * Count, 2, MidpointRounding.AwayFromZero);
            DisAmount = OriginAmount - Amount;
        }
        public decimal GetBestAmount()
        {
            return Math.Round(GetBestPrice() * Count, 2, MidpointRounding.AwayFromZero);
        }
        public decimal GetBestPrice(bool checkVipPrice = true)
        {
            return (
                PromotePrice4 > 0 ? PromotePrice4 :
                PromotePrice3 > 0 ? PromotePrice3 :
                PromotePrice2 > 0 ? PromotePrice2 :
                (VipPrice > 0 && checkVipPrice ) ? Math.Min(VipPrice, Price) :
                PromotePrice1 > 0 ? Math.Min(PromotePrice1, Price) : Math.Min(Price, OriginPrice)
            );
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); //实现通知刷新
        }
    }
}
